package com.java.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.java.model.Company;
import com.java.model.PageBean;
import com.java.model.User;
import com.java.service.CompanyService;
import com.java.util.AppConfig;
import com.java.util.PageUtil;
import com.java.util.ResponseUtil;
import com.java.util.StringUtil;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("/company")
public class CompanyController {

	@Autowired
	private CompanyService companyService;

	@RequestMapping("/list")
	public ModelAndView list(@RequestParam(value = "page", required = false) String page, Company s_company,
			HttpServletRequest request) {
		ModelAndView mav = new ModelAndView();
		HttpSession session = request.getSession();
		System.out.println(page);
		if (StringUtil.isEmpty(page)) {
			page = "1";
			session.setAttribute("s_company", s_company);
			System.out.println(s_company.getId());
		} else {
			s_company = (Company) session.getAttribute("s_company");
		}
		PageBean pageBean = new PageBean(Integer.parseInt(page), AppConfig.pageSize);
		List<Company> companyList = companyService.find(pageBean, s_company);
		int total = companyService.count(s_company);
		String pageCode = PageUtil.getPagation(request.getContextPath() + "/company/list.do", total,
				Integer.parseInt(page), AppConfig.pageSize);
		mav.addObject("pageCode", pageCode);
		User user = (User)session.getAttribute("currentUser");
		if ("使用者".equals(user.getRoleName())){
			mav.addObject("modeName", "查网点");
		}else{
			mav.addObject("modeName", "快递公司管理");
		}
		mav.addObject("companyList", companyList);
		mav.addObject("mainPage", "company/list.jsp");
		mav.setViewName("main");
		return mav;
	}

	@RequestMapping("/preSave")
	public ModelAndView preSave(@RequestParam(value = "id", required = false) String id) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("mainPage", "company/save.jsp");
		mav.addObject("modeName", "快递公司管理");
		mav.setViewName("main");
		if (StringUtil.isNotEmpty(id)) {
			mav.addObject("actionName", "快递公司修改");
			Company company = companyService.loadById(Integer.parseInt(id));
			mav.addObject("company", company);
		} else {
			mav.addObject("actionName", "快递公司添加");
		}
		return mav;
	}

	@RequestMapping("/save")
	public String save(Company company) {
		if (company.getId() == null) {
			companyService.add(company);
		} else {
			companyService.update(company);
		}
		return "redirect:/company/list.do";
	}

	@RequestMapping("/delete")
	public void delete(@RequestParam(value = "id") String id, HttpServletResponse response) throws Exception {
		JSONObject result = new JSONObject();
		companyService.delete(Integer.parseInt(id));
		result.put("success", true);
		ResponseUtil.write(result, response);
	}
}
